package com.ash.android_adk_demo
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.hc.schedule.model.Schedule

class ScheduleAdapter(private var schedules: List<Schedule>) : 
    RecyclerView.Adapter<ScheduleAdapter.ScheduleViewHolder>() {

    
    private var onItemClickListener: ((Schedule) -> Unit)? = null

    fun setOnItemClickListener(listener: (Schedule) -> Unit) {
        onItemClickListener = listener
    }
    

    fun updateData(newSchedules: List<Schedule>) {
        schedules = newSchedules
        notifyDataSetChanged()
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ScheduleViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_schedule, parent, false)
        return ScheduleViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: ScheduleViewHolder, position: Int) {
        val schedule = schedules[position]
        holder.bind(schedule)
    }
    
    override fun getItemCount(): Int = schedules.size
    
    inner class ScheduleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val nameTextView: TextView = itemView.findViewById(R.id.tv_schedule_name)
        private val briefTextView: TextView = itemView.findViewById(R.id.tv_schedule_brief)
        private val isAddTextView: TextView = itemView.findViewById(R.id.tv_isadd)
        private val daysTextView: TextView = itemView.findViewById(R.id.tv_days)
        
        fun bind(schedule: Schedule) {
            nameTextView.text = schedule.name
            briefTextView.text = schedule.brief
            
            val totalDay = schedule.totalDay
            daysTextView.text = "$totalDay 天"
            
            // 显示订阅状态
            isAddTextView.text = if (schedule.isSubscribed) "已订阅" else "未订阅"
            
            
            // 设置点击事件，弹出带有三个按钮的对话框
            itemView.setOnClickListener {
                onItemClickListener?.invoke(schedule)
            }
        }

    }
}